import { SchemaElement } from "./schema-element";

const getOrCreateElementData = (element: SchemaElement): Record<string, any> => {
    let data = element.data;
    if (!data) {
        data = {};
        element.data = data;
    }
    return data;
}

const getOrCreateArrayProp = <T>(element: SchemaElement, propName: string): Array<T> => {
    const data = getOrCreateElementData(element);
    let prop = data[propName];
    if (!prop) {
        prop = [];
        data[propName] = prop;
    }
    return prop as Array<T>;
}

const empty: any[] = [];
const getArrayPropValue = <T>(element: SchemaElement, propName: string): Array<T> => {
    const data = element.data;
    if (!data) {
        return empty;
    }
    return (data[propName] || empty) as Array<T>;
}

/**
 * 添加元素开始的分隔符
 * @param element 元素
 * @param splitor 分隔符
 */
export const addStartSplitor = (element: SchemaElement, splitor: string) => {
    getOrCreateArrayProp(element, 'startSplitor').push(splitor);
}
export const addEndSplitor = (element: SchemaElement, splitor: string) => {
    getOrCreateArrayProp(element, 'endSplitor').push(splitor);
}

export const getStartSplitor = (element: SchemaElement): string[] => {
    return getArrayPropValue(element, 'startSplitor');
}
export const getEndSplitor = (element: SchemaElement): string[] => {
    return getArrayPropValue(element, 'endSplitor');
}

export const addLeftSplitor = (element: SchemaElement, splitor: string) => {
    getOrCreateArrayProp(element, 'splitorsLeft').push(splitor);
}
export const addRightplitor = (element: SchemaElement, splitor: string) => {
    getOrCreateArrayProp(element, 'splitorsRight').push(splitor);
}
export const getLeftSplitor = (element: SchemaElement): string[] => {
    return getArrayPropValue(element, 'splitorsLeft');
}
export const getRightSplitor = (element: SchemaElement): string[] => {
    return getArrayPropValue(element, 'splitorsRight');
}
